/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * login.java
 *
 * Created on 04-ene-2012, 16:21:43
 */
package incidencias;

import accesoBD.GenericQuery;
import accesoBD.QuerySupervisor;
import accesoBD.QueryTecnico;
import accesoBD.QueryUsuario;
import idiomas.messages;
import java.sql.SQLException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Cristian
 */
public class login extends javax.swing.JFrame {

    /** Creates new form login */
    public login() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        InserteLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        passLabel = new javax.swing.JLabel();
        loginButtton = new javax.swing.JButton();
        lenguageSelect = new javax.swing.JComboBox();
        lenguageLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPasswordField = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        InserteLabel.setText("Inserte su usuario y contraseña por favor.");

        emailLabel.setText("Email:");
        emailLabel.setPreferredSize(new java.awt.Dimension(70, 20));

        nameTextField.setMinimumSize(new java.awt.Dimension(10, 20));
        nameTextField.setPreferredSize(new java.awt.Dimension(100, 20));

        passLabel.setText("Passwd:");
        passLabel.setPreferredSize(new java.awt.Dimension(70, 20));

        loginButtton.setText("login");
        loginButtton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButttonActionPerformed(evt);
            }
        });

        lenguageSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Español", "English" }));
        lenguageSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lenguageSelectActionPerformed(evt);
            }
        });

        lenguageLabel.setText("Idioma:");

        jLabel3.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(92, 92, 92)
                                .addComponent(loginButtton, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)))
                        .addGap(116, 116, 116))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(passLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(emailLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jPasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                                    .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)))
                            .addComponent(InserteLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lenguageLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lenguageSelect, 0, 128, Short.MAX_VALUE)
                        .addGap(145, 145, 145))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lenguageLabel)
                    .addComponent(lenguageSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addComponent(InserteLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(emailLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loginButtton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
    * Controlador encargado de identificar al usuario y decidir el panel que le corresponda.
    * @param evt 
    */
        private void loginButttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButttonActionPerformed
        //En realidad no se introduce el nombre sino el mail.
        String[] args = {nameTextField.getText(),new String(jPasswordField.getPassword())};
        GenericQuery query;
        try {
            // TODO add your handling code here:
            jLabel3.setText("");
            query = GenericQuery.login(args[0], args[1]);
            if(QuerySupervisor.class.isInstance(query)){
                //PanelSupervisor.main(args);
                this.setVisible(false);
                PanelSupervisor.start(args, (QuerySupervisor)query);
            }else if (QueryTecnico.class.isInstance(query)){
                this.setVisible(false);
                PanelMantenimiento.main(args, (QueryTecnico)query);
            }else if (QueryUsuario.class.isInstance(query)){
                this.setVisible(false);
                PanelCliente2.main(args,(QueryUsuario)query);
            }else{
                if(query.toString().equals("usuario inexistente"))
                    jLabel3.setText(messages.getString("NoRegisterUser"));
                else if(query.toString().equals("password incorrecto"))
                    jLabel3.setText(messages.getString("ErrorPass"));
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
        }
            
    }//GEN-LAST:event_loginButttonActionPerformed
    /**
    * Controlador encargado de la lista de seleccion del idioma. 
    * Se encarga de la internacionalizacion del panel.
    * @param evt 
    */
    private void lenguageSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lenguageSelectActionPerformed
        String Idioma = (String) lenguageSelect.getSelectedItem();
        switch(lenguageSelect.getSelectedIndex()){
            case 0: //Español
                Idioma="es";
                break;
            case 1: //Ingles
                Idioma="en";
                break;
        }
        Locale locale = new Locale(Idioma);
        messages.setLocale(locale);
        lenguageLabel.setText(messages.getString("Language")+":");
        InserteLabel.setText(messages.getString("Insert"));
        passLabel.setText(messages.getString("Pass")+":");
        loginButtton.setText(messages.getString("Login"));   
        if(jLabel3.isVisible()){
            System.out.println(jLabel3.getText());
            if(jLabel3.getText().equalsIgnoreCase("usuario no registrado") 
                    || jLabel3.getText().equalsIgnoreCase("No register user"))
                    jLabel3.setText(messages.getString("NoRegisterUser"));
            else if(jLabel3.getText().equalsIgnoreCase("contraseña incorrecta") 
                    || jLabel3.getText().equalsIgnoreCase("Wrong password"))
                    jLabel3.setText(messages.getString("ErrorPass"));
        }
    }//GEN-LAST:event_lenguageSelectActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                login log = new login();
                log.setVisible(true);
                log.setResizable(false);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel InserteLabel;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPasswordField;
    private javax.swing.JLabel lenguageLabel;
    private javax.swing.JComboBox lenguageSelect;
    private javax.swing.JButton loginButtton;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel passLabel;
    // End of variables declaration//GEN-END:variables
}
